package ajo;

import robocode.HitWallEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Uses strafing and detection of enemy energy drops to avoid enemy bullets. Shoots every turn if
 * possible with bullet power proportional to enemy distance.
 * 
 * @author Joshua Antonio
 * 
 */
public class BattleBot extends Robot {
  private double enemyEnergy;
  private boolean moveForward = true;

  /**
   * Runs the bot.
   */
  public void run() {
    this.setAdjustRadarForRobotTurn(true);
    this.turnGunLeft(90);
    this.setAdjustRadarForGunTurn(false);
    while (true) {
      this.turnRadarRight(360);
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see robocode.Robot#onScannedRobot(robocode.ScannedRobotEvent)
   * 
   * @param e The event details.
   */
  @Override
  public void onScannedRobot(ScannedRobotEvent e) {
    // Square off
    if (this.getHeading() != e.getBearing()) {
      this.turnRight(e.getBearing() + 90);
    }
    if (this.getEnergy() > 5) {
      this.fire(this.getBulletPower(e.getDistance()));
    }

    if (e.getEnergy() != this.enemyEnergy) {
      if (this.moveForward) {
        this.ahead(40);
      }
      else {
        this.back(40);
      }
    }
    this.enemyEnergy = e.getEnergy();
  }

  /**
   * @param distance How far away the enemy robot is.
   * @return power
   */
  public double getBulletPower(double distance) {
    return 3.0 - distance * 0.003;
  }

  /**
   * @param e The event details.
   */
  public void onHitWall(HitWallEvent e) {
    if (this.moveForward) {
      this.moveForward = false;
    }
    else {
      this.moveForward = true;
    }
  }
}
